package top.sdaily.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.sdaily.core.mybatis.Conditions;
import top.sdaily.core.utils.IdGeneratorUtils;
import top.sdaily.mapper.MenuMapper;
import top.sdaily.mapper.RoleMenuMapper;
import top.sdaily.mapper.RoleMenuRightMapper;
import top.sdaily.web.dto.DRoleMenu;
import top.sdaily.web.model.Menu;
import top.sdaily.web.service.MenuService;

import java.util.List;

/**
 * Created by soya on 2016/10/29.
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;
    @Autowired
    RoleMenuMapper roleMenuMapper;
    @Autowired
    RoleMenuRightMapper roleMenuRightMapper;

    @Override
    public List<DRoleMenu> getMenusByRole(String roleId) {
        List<DRoleMenu> rootMenus = menuMapper.findByRole(roleId,"0");
        return rootMenus;
    }

    @Override
    public List<Menu> findAll() {
        return menuMapper.findTree("0");
    }

    @Override
    public void create(Menu menu) {
        menu.setPkid(IdGeneratorUtils.fresh());
        menu.setShow(1);
        menuMapper.insert(menu);
    }

    @Override
    public void modify(Menu menu) {
        menuMapper.update(menu);
    }

    @Override
    public void delete(String pkid) {
        menuMapper.delete(new Conditions(Menu.class).id(pkid));
        menuMapper.delete(new Conditions(Menu.class).eq("parent_id",pkid));
    }
}
